(function() {
	window._color={
			splitRGB:function(rgb){
				return rgb.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");
			},
			rgb2hex:function(rgb){
				var aColor = _color.splitRGB(rgb);
				var strHex = "#";
				for(var i=0; i<aColor.length; i++){
					var num=Number(aColor[i]);
					var hex=num<16?"0"+num.toString(16):num.toString(16);
					strHex += hex;
				}
				if(strHex.length !== 7){
					strHex = rgb;	
				}
				return strHex;
			},
			hex2rgb:function(hex){
				var sColor = hex.toLowerCase();
				if(sColor.length === 4){
					var sColorNew = "#";
					for(var i=1; i<4; i+=1){
						sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));	
					}
					sColor = sColorNew;
				}
				//处理六位的颜色值
				var sColorChange = [];
				for(var i=1; i<7; i+=2){
					sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));	
				}
				return "rgb(" + sColorChange.join(",") + ")";
			},
			/**
			 * RGB 颜色值转换为 HSL.
			 * 转换公式参考自 http://en.wikipedia.org/wiki/HSL_color_space.
			 * r, g, 和 b 需要在 [0, 255] 范围内
			 * 返回的 h, s, 和 l 在 [0, 1] 之间
			 *
			 * @param   Number  r       红色色值
			 * @param   Number  g       绿色色值
			 * @param   Number  b       蓝色色值
			 * @return  Array           HSL各值数组
			 */
			rgb2hsl:function(r,g,b){
				r /= 255, g /= 255, b /= 255;
				var max = Math.max(r, g, b), min = Math.min(r, g, b);
				var h, s, l = (max + min) / 2;

				if(max == min){
					h = s = 0; // achromatic
				}else{
					var d = max - min;
					s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
					switch(max){
						case r: h = (g - b) / d + (g < b ? 6 : 0); break;
						case g: h = (b - r) / d + 2; break;
						case b: h = (r - g) / d + 4; break;
					}
					h /= 6;
				}

				return [h, s, l];
			},
			hsl2rgb:function(h,s,l){
				s=s>1?s/100:s;
				l=l>1?l/100:l;
				var r, g, b;

				if(s == 0){
					r = g = b = l; // achromatic
				}else{
					var hue2rgb = function hue2rgb(p, q, t){
						if(t < 0) t += 1;
						if(t > 1) t -= 1;
						if(t < 1/6) return p + (q - p) * 6 * t;
						if(t < 1/2) return q;
						if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
						return p;
					}

					var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
					var p = 2 * l - q;
					r = hue2rgb(p, q, h + 1/3);
					g = hue2rgb(p, q, h);
					b = hue2rgb(p, q, h - 1/3);
				}

				return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
			}
			
			
			
	}
}.call(this));


if ( typeof define === "function") {
	define( "_color", [], function () { return _color; } );
}

